<template>
    <Row>
        <Col span="22" offset="1">
            <Form ref="Project" :model="Project" :rules="ruleValidate" :label-width="120" :style="{marginTop: '30px'}">
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="项目编码">
                            <Input v-model.trim="Project.NumCode" readonly placeholder="项目编码由系统自动生成" ></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Divider orientation="left">项目地区信息</Divider>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="省份" prop="Province">
                            <!-- <Input v-model.trim="Project.Province" placeholder="例如：浙江省、湖南省、上海市" ></Input> -->
                            <Select v-model.trim="Project.Province" :loading="ProvinceStatus" :remote-method="SelectProvince" @on-open-change="ProvinceChanged" filterable clearable placeholder="请选择省份">
                                <Option v-for="item in Province" :value=item.value :key="item.Id">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col span="10" >
                        <FormItem label="城市" prop="City">
                            <!-- <Input v-model.trim="Project.City" placeholder="例如：绍兴市、杭州市、宁波市" ></Input> -->
                            <Select v-model.trim="Project.City" :loading="CityStatus" :remote-method="SelectCity" @on-open-change="CityChanged" filterable clearable placeholder="请选择城市">
                                <Option v-for="item in City" :value=item.value :key="item.Id">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="市（县）、区"  prop="Zone">
                            <!-- <Input v-model.trim="Project.Zone" placeholder="例如：诸暨市、丽水市、绍兴县" ></Input> -->
                            <Select v-model.trim="Project.Zone" :loading="ZoneStatus" :remote-method="SelectZone" @on-open-change="ZoneChanged" filterable clearable placeholder="请选择市（县）、区">
                                <Option v-for="item in Zone" :value=item.value :key="item.Id">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col span="10" >
                        <FormItem label="分公司" prop="Company">
                            <!-- <Input v-model.trim="Project.Company" placeholder="例如：绍兴分公司、杭州分公司、宁波分公司" ></Input> -->
                            <Select v-model.trim="Project.Company" filterable >
                                <Option v-for="item in Companys" :value=item.value :key="item.value">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
                <Divider orientation="left">项目基本信息</Divider>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="项目名称" prop="Name">
                            <Input  v-model.trim="Project.Name" placeholder="例如：杭州之江云镜二标" ></Input>
                                <!-- <Button slot="append" @click="SelectProject = true" icon="ios-search"></Button> -->
                            
                        </FormItem>
                    </Col>
                    <Col span="10" >
                        <FormItem label="工程类型" prop="ItemType">
                            <!-- <Input v-model.trim="Project.ItemType" placeholder="例如：土建、消防、涂料、其他" ></Input> -->
                            <Select v-model.trim="Project.ItemType" filterable >
                                <Option v-for="item in ItemTypes" :value=item.value :key="item.value">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col span="10" offset="2">
                        <FormItem label="项目财务" prop="Accountant">
                            <Input v-model.trim="Project.Accountant" placeholder="例如：罗卿" ></Input>
                                <!-- <Button slot="append" @click="SelectAccountant = true" icon="ios-search"></Button> -->
                            
                        </FormItem>
                    </Col>
                    <Col span="10" >
                        <FormItem label="项目地块" prop="LandParcel">
                            <Input v-model.trim="Project.LandParcel" placeholder="例如：XXX #地块" ></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="20" offset="2">
                        <FormItem label="项目地址：" prop="DetailAddress">
                            <Input  v-model.trim="Project.DetailAddress" placeholder="例如：浙江省杭州市XXX" ></Input>
                            <!-- <Button slot="append" @click="modal1 = true" icon="ios-search"></Button> -->
                        </FormItem>
                    </Col>
                </Row>
                <Divider orientation="left">项目资金信息</Divider>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="项目余额" prop="BeginBalance">
                            <Input v-model.number="Project.BeginBalance" placeholder="例如：10000.00" >
                                <span slot="suffix"><i>元</i ></span>
                                <Icon type="logo-yen" slot="prefix" />
                            </Input>
                        </FormItem>
                    </Col>
                    <Col span="10">
                        <FormItem label="冻结资金" prop="FreezeMoney">
                            <Input v-model.number="Project.FreezeMoney" placeholder="例如：10000.00" >
                                <span slot="suffix"><i>元</i ></span>
                                <Icon type="logo-yen" slot="prefix" />
                            </Input>
                        </FormItem>
                    </Col>
                    <Col span="10" offset="2">
                        <FormItem label="可用余额" prop="FlowBalance">
                            <Input v-model.number="Project.FlowBalance" readonly placeholder="例如：10000.00" >
                                <span slot="suffix"><i>元</i ></span>
                                <Icon type="logo-yen" slot="prefix" />
                            </Input>
                        </FormItem>
                    </Col>
                    <Col span="10">
                        <FormItem label="现金余额" prop="CashBalance">
                            <Input v-model.number="Project.CashBalance" readonly placeholder="例如：10000.00" >
                                <span slot="suffix"><i>元</i ></span>
                                <Icon type="logo-yen" slot="prefix" />
                            </Input>
                        </FormItem>
                    </Col>
                    <Col span="10" offset="2">
                        <FormItem label="商票余额" prop="CommercialTicket">
                            <Input v-model.number="Project.CommercialTicket" placeholder="例如：10000.00" >
                                <span slot="suffix"><i>元</i ></span>
                                <Icon type="logo-yen" slot="prefix" />
                            </Input>
                        </FormItem>
                    </Col>
                    <Col span="10">
                        <FormItem label="工抵房" prop="GongDiFang">
                            <Input v-model.number="Project.GongDiFang" placeholder="例如：10000.00" >
                                <span slot="suffix"><i>元</i ></span>
                                <Icon type="logo-yen" slot="prefix" />
                            </Input>
                        </FormItem>
                    </Col>
                </Row>
                <Divider orientation="left">其他信息</Divider>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="启用标记">
                            <Select v-model.number="Project.IsUsed" >
                                <Option v-for="item in IsUsed" :value=item.value :key="item.label">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col span="10" >
                        <FormItem label="创建时间">
                            <DatePicker v-model="Project.CreateTime" type="date" placeholder="请选择日期" format="yyyy年MM月dd日 HH时mm分ss秒" readonly style="width:100%"></DatePicker>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="20" offset="2" style="text-align:center;margin-top:58px">
                        <Button type="primary" @click="Submit('Project')">提交</Button>
                        <Button @click="Reset('Project')" style="margin-left: 108px">重置</Button>
                    </Col>
                </Row>
            </Form>
            <Modal
                v-model="SelectProject"
                title="请选择项目"
                @on-ok="ok"
                @on-cancel="cancel">
                <p>Content of dialog</p>
                <p>Content of dialog</p>
                <p>Content of dialog</p>
            </Modal>
            <Modal
                v-model="SelectAccountant"
                title="请选择项目财务"
                @on-ok="ok"
                @on-cancel="cancel">
                <p>Content of dialog</p>
                <p>Content of dialog</p>
                <p>Content of dialog</p>
            </Modal>
        </Col>
    </Row>
</template>
<script>
import { mapState } from "vuex";
import { CreateCode,GetOne,Create,Update,GetCompanyList,GetItemTypeList } from '@/axios/api/ProjectAPI'
import { GetProvinceList,GetCityList,GetZoneList } from '@/axios/api/AdministrativeAPI'

export default {
    computed:{
        ...mapState('project',[
            'currentProjectID',
            'pageIndex',
            'pageSize',
        ])
    },
    components: {
        
    },
    mounted: function(){
        //console.log(this.currentProjectID)

        if(this.currentProjectID != ''){
            this.$Spin.show();
        }
        setTimeout(() => {
            this.$Spin.hide();
        }, 1000);

        if(this.currentProjectID != ''){
            GetOne(this.currentProjectID).then(res => {
                this.Project.Id = res.Id;
                this.Project.Num = res.Num;
                this.Project.NumCode = res.NumCode;
                this.Project.Province = res.Province;
                this.Project.City = res.City;
                this.Project.Zone = res.Zone;
                this.Project.Company = res.Company;
                this.Project.Name = res.Name;
                this.Project.ItemType = res.ItemType;
                this.Project.Accountant = res.Accountant;
                this.Project.LandParcel = res.LandParcel;
                this.Project.DetailAddress = res.DetailAddress;
                this.Project.SettlementProjectId = res.SettlementProjectId;
                this.Project.SettlementPeriod = res.SettlementPeriod;
                this.Project.BeginTime = res.BeginTime;
                this.Project.EndTime = res.EndTime;
                this.Project.BeginBalance = res.BeginBalance;
                this.Project.FreezeMoney = res.FreezeMoney;
                this.Project.FlowBalance = res.FlowBalance;
                this.Project.CashBalance = res.CashBalance;
                this.Project.CommercialTicket = res.CommercialTicket;
                this.Project.GongDiFang = res.GongDiFang;
                this.Project.IsSettlement = res.IsSettlement;
                this.Project.IsUsed = res.IsUsed;
                this.Project.CreateTime = res.CreateTime;
            });
        }
        else{
            CreateCode().then(res => {
                this.Project.NumCode = res.toString();
            })
        }

        GetCompanyList().then(res => {
            const list = res.map(item => {
                return {
                    value: item.Id,
                    label: item.Name,
                    Id: item.Id
                };
            });
            this.Companys = list;
        })

        GetItemTypeList().then(res => {
            const list = res.map(item => {
                return {
                    value: item.Code,
                    label: item.Name,
                    Id: item.Id
                };
            });
            this.ItemTypes = list;
        })

        //加载省、市、县
        setTimeout(() => {
            GetProvinceList().then(res => {
                const list = res.map(item => {
                    return {
                        value: item.Code,
                        label: item.Name,
                        Id: item.Id
                    };
                });
                this.Province = list;

            setTimeout(() => {
                GetCityList(this.Project.Province).then(res => {
                    const list = res.map(item => {
                        return {
                            value: item.Code,
                            label: item.Name,
                            Id: item.Id
                        };
                    });
                    this.City = list;

                    setTimeout(() => {
                        GetZoneList(this.Project.City).then(res => {
                                const list = res.map(item => {
                                    return {
                                        value: item.Code,
                                        label: item.Name,
                                        Id: item.Id
                                    };
                                });
                                this.Zone = list;
                            })
                        }, 50);
                    })
                }, 50);
            })
        }, 50);
    },
    data () {
        return {
            Project: {
                Id: '00000000-0000-0000-0000-000000000000',
                Num: 1,
                NumCode: '',
                Province: '',
                City: '',
                Zone: '',
                Name: '',
                Company: '00000000-0000-0000-0000-000000000000',
                ItemType: '',
                Accountant: '',
                LandParcel: '',
                DetailAddress: '',
                SettlementProjectId: '00000000-0000-0000-0000-000000000000',
                SettlementPeriod: 0,
                BeginTime: new Date(),
                EndTime: new Date(),
                BeginBalance: 0.0,
                EndBalance: 0.0,
                FreezeMoney: 0.0,
                FlowBalance: 0.0,
                CashBalance: 0.0,
                CommercialTicket: 0.0,
                GongDiFang: 0.0,
                IsSettlement: false,
                IsUsed: 1 ,
                CreateTime: new Date()
            },
            ruleValidate: {
                Num: [{ type: 'number', message: '只能输入数字', trigger: 'blur' }],
                Province: [{ required: true, message: '项目所在省份为必填！', trigger: 'blur'  }],
                City: [{ required: true, message: '项目所在地级市为必填！', trigger: 'blur'  }],
                Zone: [{ required: true, message: '项目所在县级市为必填！', trigger: 'blur'  }],
                Company: [{ required: true, message: '项目所在分公司为必填！', trigger: 'blur'  }],
                Name: [{ required: true, message: '项目名称为必填项！', trigger: 'blur'  }],
                BeginBalance: [{ type: 'number', message: '只能输入数字', trigger: 'blur' }],
                EndBalance: [{ type: 'number', message: '只能输入数字', trigger: 'blur' }]
            },
            IsUsed:[
                {
                    value: 1,
                    label: "启用"
                },
                {
                    value: 0,
                    label: "禁用"
                }
            ],
            callmodal: "project-sheet",
            Companys:[],
            SelectProject: false,
            SelectAccountant: false,
            Province:[],
            City:[],
            Zone:[],
            ItemTypes: [],
            ProvinceStatus: false,
            CityStatus: false,
            ZoneStatus: false
        }
    },
    watch: {
        'Project.BeginBalance'(value){
            if(value != '' && value != null){
                this.Project.FlowBalance = value - this.Project.FreezeMoney;
                this.Project.CashBalance = this.Project.FlowBalance - this.Project.CommercialTicket - this.Project.GongDiFang;
            }
        },
        'Project.FreezeMoney'(value){
            if(value != '' && value != null){
                this.Project.FlowBalance = this.Project.BeginBalance - value;
                this.Project.CashBalance = this.Project.FlowBalance - this.Project.CommercialTicket - this.Project.GongDiFang;
            }
        },
        'Project.CommercialTicket'(value){
            if(value != '' && value != null){
                this.Project.FlowBalance = this.Project.BeginBalance - this.Project.FreezeMoney;
                this.Project.CashBalance = this.Project.FlowBalance - value - this.Project.GongDiFang;
            }
        },
        'Project.GongDiFang'(value){
            if(value != '' && value != null){
                this.Project.FlowBalance = this.Project.BeginBalance - this.Project.FreezeMoney;
                this.Project.CashBalance = this.Project.FlowBalance - this.Project.CommercialTicket - value;
            }
        }
    },
    methods: {
        Submit (name){
            //this.$Message.success('按了OK键');
             this.$refs[name].validate((valid) => {
                if (valid) {
                    if(this.Project.Id == '00000000-0000-0000-0000-000000000000'){
                        this.$Message.success('校验通过!');
                        Create(this.Project).then(res => {
                            if(res > 0){

                                setTimeout(() => {
                                    this.$store.dispatch('project/UpdateTableAsync',{
                                        PageIndex: this.pageIndex,
                                        PageSize: this.pageSize
                                    });

                                        this.$Notice.success({
                                        title: '操作提示',
                                        desc: '保存成功！ '
                                    });
                                }, 1000)
                                
                            }
                            else{
                                this.$Notice.error({
                                    title: '操作提示',
                                    desc: '保存失败！ '
                                });
                            }
                        }).catch(err =>{
                            this.$Notice.error({
                                title: '操作提示',
                                desc: '保存失败：' + err.data + '!'
                            });
                        })
                        
                    }
                    else{
                        this.$Message.success('校验通过!');
                        
                        Update(this.Project).then(res => {
                            if(res > 0){
                                setTimeout(() => {
                                    this.$store.dispatch('project/UpdateTableAsync',{
                                        PageIndex: this.pageIndex,
                                        PageSize: this.pageSize
                                    });

                                        this.$Notice.success({
                                        title: '操作提示',
                                        desc: '更新成功！'
                                    });
                                }, 1000)
                            }
                            else{
                                this.$Notice.success({
                                    title: '操作提示',
                                    desc: '更新失败！'
                                });
                            }
                        }).catch(err =>{
                            this.$Notice.error({
                                title: '操作提示',
                                desc: '更新失败：' + err.data + '!'
                            });
                        })
                    }
                    this.ChangeModal();
                } else {
                    this.$Message.error('校验失败!');
                    //this.ChangeModal('Project');
                }
            })
            
        },
        Reset (name){
            console.log(this.$refs[name]);
            this.$refs[name].resetFields();
        },
        ChangeModal (){
            //this.$refs[name].resetFields();
            this.$store.commit('project/CHANGE_MODAL');
            this.$router.push('/funds/Project')
        },
        SelectProvince (query){
            if(query != ''){
                this.ProvinceStatus = true;
                setTimeout(() => {
                    this.ProvinceStatus = false;
                    GetProvinceList().then(res => {
                        const list = res.map(item => {
                            return {
                                value: item.Code,
                                label: item.Name,
                                Id: item.Id
                            };
                        });
                        this.Province = list.filter(item => item.label.indexOf(query) > -1);
                    })
                }, 200);
            }
        },
        ProvinceChanged(IsOpen){
            if(IsOpen){
                GetProvinceList().then(res => {
                    const list = res.map(item => {
                        return {
                            value: item.Code,
                            label: item.Name,
                            Id: item.Id
                        };
                    });
                    this.Province = list;
                })
                this.Project.City = '';
                this.Project.Zone = '';
            }
            
        },
        SelectCity (query){
            if(query !== ''){
                this.CityStatus = true;
                setTimeout(() => {
                    this.CityStatus = false;
                    GetCityList(this.Project.Province).then(res => {
                        const list = res.map(item => {
                            return {
                                value: item.Code,
                                label: item.Name,
                                Id: item.Id
                            };
                        });
                        this.City = list.filter(item => item.label.indexOf(query) > -1);
                    })
                }, 200);
            }
        },
        CityChanged(IsOpen){
            if(IsOpen){
                GetCityList(this.Project.Province).then(res => {
                    const list = res.map(item => {
                        return {
                            value: item.Code,
                            label: item.Name,
                            Id: item.Id
                        };
                    });
                    this.City = list;
                })
                this.Project.Zone = '';
            }
        },
        SelectZone (query){
            if(query !== ''){
                this.ZoneStatus = true;
                setTimeout(() => {
                    this.ZoneStatus = false;
                    GetZoneList(this.Project.City).then(res => {
                        const list = res.map(item => {
                            return {
                                value: item.Code,
                                label: item.Name,
                                Id: item.Id
                            };
                        });
                        this.Zone = list.filter(item => item.label.indexOf(query) > -1);
                    })
                }, 200);
            }
        },
        ZoneChanged(){
            GetZoneList(this.Project.City).then(res => {
                const list = res.map(item => {
                    return {
                        value: item.Code,
                        label: item.Name,
                        Id: item.Id
                    };
                });
                this.Zone = list;
            })
        }
    }
}
</script>
